import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';

/// description: 自定义筛选底部弹窗 选择器
/// @Param [dataList] 选择器数据
/// @Param [onConfirm] 确认回调
class CustomFilterBottomSheet extends StatefulWidget {
  const CustomFilterBottomSheet({
    required this.dataList,
    required this.onConfirm,
    super.key,
    this.showValue = '',
    this.isRadio = true,
  });

  /// 选择器数据
  final List<FilterBean> dataList;

  /// 确认回调
  final Function(List<FilterBean>) onConfirm;

  ///列表上显示的字段
  final String? showValue;

  /// 是否单选
  final bool isRadio;

  @override
  State<CustomFilterBottomSheet> createState() => _CustomFilterBottomSheetState();
}

class _CustomFilterBottomSheetState extends State<CustomFilterBottomSheet> {
  List<FilterBean> _copyFilterList = [];

  /// 选择点击事件
  void onTap(FilterBean bean, FilterEntitiesBean item) {
    //如果是单选
    if (widget.isRadio) {
      for (var selectItem in bean.filterList) {
        //清空之前选中的数据
        selectItem.isChosen = false;
      }
      item.isChosen = true;
    } else {
      item.isChosen = !item.isChosen;
    }
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    //复制一份数据
    _copyFilterList = widget.dataList
        .map((bean) => FilterBean(
            filterList: bean.filterList
                .map((filterBean) => FilterEntitiesBean(
                      name: filterBean.name,
                      value: filterBean.value,
                      isChosen: filterBean.isChosen,
                    ))
                .toList(),
            title: bean.title,
            chosenValue: []))
        .toList();
  }

  //每一项筛选所显示的内容
  Widget _getFilterView(FilterBean bean) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          bean.title,
          style: TextStyle(color: const Color(0xff333333), fontSize: 14.w, fontWeight: FontWeight.w500),
        ),
        AppGaps.vGap8,
        Wrap(
          children: bean.filterList
              .map((entitiesBean) => GestureDetector(
                  onTap: () {
                    onTap(bean, entitiesBean);
                    // entitiesBean.isChosen = !entitiesBean.isChosen;
                    // setState(() {});
                  },
                  child: Container(
                    margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 6.w),
                    height: 32.w,
                    width: 90.w,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(16.w)),
                      color: entitiesBean.isChosen ? const Color(0xff5C6DFF) : const Color(0xffF8F8F8),
                      border: Border.all(color: const Color(0xffF0F0F0), width: 1.w),
                    ),
                    alignment: Alignment.center,
                    child: Text(
                      entitiesBean.name,
                      style: TextStyle(
                          color: entitiesBean.isChosen ? const Color(0xffffffff) : const Color(0xff999999), fontSize: 12.w, fontWeight: FontWeight.w400),
                    ),
                  )))
              .toList(),
        ),
        AppGaps.vGap20,
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(topLeft: Radius.circular(20.w), topRight: Radius.circular(20.w)),
        color: Colors.white,
      ),
      child: Column(
        children: [
          AppGaps.vGap20,
          Text(
            '筛选',
            style: TextStyle(
              fontSize: 18.sp,
              fontWeight: FontWeight.w500,
              color: Colors.black,
            ),
          ),
          AppGaps.vGap20,
          Expanded(
              child: SingleChildScrollView(
            padding: EdgeInsets.zero,
            child: Column(
              children: _copyFilterList.map((filterBean) {
                return Container(
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  alignment: Alignment.topLeft,
                  child: _getFilterView(filterBean),
                );
              }).toList(),
            ),
          )),
          Container(
            height: 8.w,
            width: AppDimens.sw,
            color: const Color(0xFFF2F2F2),
          ),
          Container(
              height: 40.w,
              width: AppDimens.sw,
              alignment: Alignment.center,
              margin: EdgeInsets.symmetric(horizontal: 16.w),
              decoration: BoxDecoration(color: const Color(0xff5C6DFF), borderRadius: BorderRadius.all(Radius.circular(20.w))),
              child: Text(
                '确定',
                style: TextStyle(
                  fontSize: 18.sp,
                  color: Colors.white,
                ),
              )).clickOnce(() {
            for (var value in _copyFilterList) {
              if (value.filterList.isNotEmpty) {
                for (var element in value.filterList) {
                  if (element.isChosen) {
                    value.chosenValue.add(element);
                  }
                }
              }
            }
            widget.onConfirm(_copyFilterList);
            Get.back();
          }),
          AppGaps.vGap10,
        ],
      ),
    );
  }
}
